/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import Connexion.ConnexionCustomer;
import actions.facade.FacadeAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.rmi.RemoteException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import miage.beans.ClientPresentation;
import miage.sessions.ecom.remote.EcomCustomerRemote;


/**
 *
 * @author Pepito
 */
public class ClientAction extends FacadeAction {
    
    private static EcomCustomerRemote rechercher;
    private Map session = ActionContext.getContext().getSession();    
    /*
     * Donner du client
     */
    private String iban, nom, prenom, password, login, email,adresse_client,codepostal_client,ville_client,pagePanier;
    private double solde;

    public String getAdresse_client() {
        return adresse_client;
    }

    public String getCodepostal_client() {
        return codepostal_client;
    }

    public void setCodepostal_client(String codepostal_client) {
        this.codepostal_client = codepostal_client;
    }

    public String getVille_client() {
        return ville_client;
    }

    public void setVille_client(String ville_client) {
        this.ville_client = ville_client;
    }

    public double getSolde() {
        return solde;
    }

    public void setSolde(double solde) {
        this.solde = solde;
    }

    public void setAdresse_client(String adresse_client) {
        this.adresse_client = adresse_client;
    }
    
    
    public String getIban() {
        return iban;
    }

    public void setIban(String iban) {
        this.iban = iban;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }  

    public String getPagePanier() {
        return pagePanier;
    }

    public void setPagePanier(String pagePanier) {
        this.pagePanier = pagePanier;
    }
    
   

    @Override
    // toujours un succès !
    public String execute( ) {
        //ClientPresentation client = new ClientPresentation(iban,nom,prenom,password,login,email,solde,null);
        //System.out.println(client.toString());
        System.out.println("aJOUT CLIENT VA COMMENCER");
        rechercher =ConnexionCustomer.connectionServerBean();
        System.out.println(rechercher.toString());
        try {
            rechercher.addClient(new ClientPresentation(this.iban, this.nom,this.prenom, 
                    this.password, this.login, this.email, this.adresse_client,
                    this.codepostal_client, this.ville_client, this.solde, "c"));
            return ActionSupport.SUCCESS;
        } catch (CreateException ex) {
            Logger.getLogger(ClientAction.class.getName()).log(Level.SEVERE, null, ex);
            return ActionSupport.INPUT;
        } catch (FinderException ex) {
            Logger.getLogger(ClientAction.class.getName()).log(Level.SEVERE, null, ex);
            return ActionSupport.INPUT;
        } catch (RemoteException ex) {
            Logger.getLogger(ClientAction.class.getName()).log(Level.SEVERE, null, ex);
            return ActionSupport.INPUT;
        }
     
        
    }
    
     public String update(){
        try {
            rechercher = ConnexionCustomer.connectionServerBean();
            System.out.println("iban : "+iban);
            ClientPresentation cp = new ClientPresentation();
            cp.setAdresse_client(adresse_client);
            cp.setCp_client(ville_client);
            cp.setEmail(email);
            cp.setFlag_client_admin("c");
            cp.setIban_client(iban);
            cp.setLogin(login);
            cp.setNom_client(nom);
            cp.setPassword(password);
            cp.setPrenom_client(prenom);
            cp.setSolde(solde);
            cp.setVille_client(ville_client);
            System.out.println(cp.toString());
            rechercher.updateClient(cp);
            
            session = ActionContext.getContext().getSession();
            
            session.put("connect", true);
            session.put("login", cp.getLogin());
            session.put("password", cp.getPassword());
            session.put("id", cp.getIban_client());
            session.put("statut", cp.getFlag_client_admin()); 
            
            return "success";
        } catch (FinderException ex) {
            Logger.getLogger(ClientAction.class.getName()).log(Level.SEVERE, null, ex);
            return "input";
        } catch (RemoteException ex) {
            Logger.getLogger(ClientAction.class.getName()).log(Level.SEVERE, null, ex);
             return "input";
        }
    }
}
